const mongoose = require("mongoose");
var User = mongoose.model("user");


exports.getUser = function(req,res){
	User.findOne(req.query,'-password',function(err,user){
		if(!err){
			req.session.username = user.username;
			res.json(user);
		}else{
			res.json({error: "can't get user by " + req.query.username + " " + req.query.password});
		}
	})
}

exports.addUser = function(req,res){
	var user = new User();
	user.username = req.body.username;
	user.password = req.body.password;
	user.save(function(err){
		if(!err){
			user.password = '';
			req.session.username = user.username;
			res.json(user);
		}else{
			res.json({error: "can't add User"})
		}
	})
}
exports.logOut = function(req,res){
	req.session.username = undefined;
	res.json({});
}